package com.baili.dao.inspector;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.baili.dao.base.BaseDao;
import com.baili.dto.Call;
import com.baili.dto.Check;
import com.baili.dto.Defect;
import com.baili.dto.ProductOrder;
import com.baili.dto.Sampling;
import com.baili.dto.common.UserConstants;
import com.baili.util.StringUtil;

/** 
 * @author iceX
 * @version 创建时间：2016年3月31日 下午3:03:29 
 * @Name: InspectorDaoImpl.java
 */
@Repository("InspectorDao")
public class InspectorDaoImpl extends BaseDao implements InspectorDao{

	@Override
	public List getEventType() {
		
		return this.sqlSessionTemplate.selectList("inspector.getEventTypeList");
	}

	@Override
	public void addEventType(String eventTopicId, String eventDescription ,String callTime,String fileName) {
		Map map = new HashMap();
		map.put("eventTopicId", eventTopicId);
		map.put("eventDescription", eventDescription);
		map.put("callTime", callTime);
		map.put("fileName", fileName);
		this.sqlSessionTemplate.insert("inspector.addEventType" ,map);
	}

	@Override
	public List getcallTypeList() {
		
		return this.sqlSessionTemplate.selectList("inspector.getcallTypeList");
	}

	@Override
	public List getmyFaultList(ProductOrder productOrder) {
		
		return this.sqlSessionTemplate.selectList("inspector.getmyFaultList",productOrder);
	}

	@Override
	public int getmyFaultCount() {
		List list = this.sqlSessionTemplate.selectList("inspector.getmyFaultCount");
		int i = list.size();
		return i;
	}

	@Override
	public List getmyFaultListByTime(String startTime, String endTime,Integer userId,String productCode,int checkProcess) {
		Map map = new HashMap();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("userId", userId);
		map.put("productCode", productCode);
		map.put("checkProcess", checkProcess);
		
		List<Map<String,Object>> list = this.sqlSessionTemplate.selectList("inspector.getmyFaultListByTime" ,map);
		
		int total = 0;
		if(list != null && list.size()>0){
			
			for(Map<String,Object> mapTotal :list){
				
				if(mapTotal != null ){
					
					total = total + Integer.parseInt(mapTotal.get("defectCount").toString());
					
				}
			}
		}
		
		for (Map<String, Object> mapTotal : list) {

			mapTotal.put("totalCount", total);

		}
				
		return list;
	}

	@Override
	public int getmyFaultCountByTime(String startTime, String endTime,Integer userId,String productCode) {
		Map map = new HashMap();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("userId", userId);
		map.put("productCode", productCode);
		List<Map<String,Object>> list = this.sqlSessionTemplate.selectList("inspector.getmyFaultCountByTime" ,map);
		int total = 0;
		if(list != null && list.size()>0){
			
			if(list.get(0) != null ){
				
				total = Integer.parseInt(list.get(0).get("defectCount").toString());
				
			}
			
			
		}
		return total;
	}
	
	@Override
	public List getCallList() {
		
		return this.sqlSessionTemplate.selectList("inspector.getCallList");
	}

	@Override
	public int updateCheakState(Integer state, Integer id) {
		Map map = new HashMap();
		map.put("state", state);
		map.put("id", id);
		this.sqlSessionTemplate.update("inspector.updateCheakState", map);
		int update = 0;
		return update;
	}

	@Override
	public List getFaultList() {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplate.selectList("inspector.getFaultList");
	}

	@Override
	public int updateFaultState(Integer state, Integer id,Integer checkId,Integer stage,Integer kzType,String processName) {
		
		Map map = new HashMap();
		
		map.put("state", state);
		
		map.put("id", id);
		
		map.put("checkId", checkId);
		
		map.put("stage", stage);
		
		map.put("kzType", kzType);
		
		int update = 0;
		
		if(processName.equals("后段")){
			
			map.put("realOverTime", new Date());
			
			this.sqlSessionTemplate.update("inspector.updateFaultStateOverStep", map);
			
		}else{
			
			this.sqlSessionTemplate.update("inspector.updateFaultState", map);
		}
		
		return update;
	}
	@Override
	public int updateFaultState(Integer state, Integer id,Integer checkId,Integer stage,Integer kzType,String processName,int type) {
		
		Map map = new HashMap();
		
		map.put("state", state);
		
		map.put("id", id);
		
		map.put("checkId", checkId);
		
		map.put("stage", stage);
		
		map.put("kzType", kzType);
		
		int update = 0;
		
		if(processName.equals("前段") || processName.equals("中段") || processName.equals("后段")){
			
			map.put("realOverTime", new Date());
			
			if(type == 1){
				
				map.put("frontType", 1);
				
				this.sqlSessionTemplate.update("inspector.updateFaultStateOverStepFront", map);
			}else if(type == 2){
				
				map.put("middleType", 1);
				
				this.sqlSessionTemplate.update("inspector.updateFaultStateOverStepMiddle", map);
				
			}else if(type == 3){
				
				map.put("afterType", 1);
				
				map.put("realOverTime", new Date());
				
				this.sqlSessionTemplate.update("inspector.updateFaultStateOverStepAfter", map);
				
			}
			
			//this.sqlSessionTemplate.update("inspector.updateFaultStateOverStep", map);
			
		}else{
			
			this.sqlSessionTemplate.update("inspector.updateFaultState", map);
		}
		
		return update;
	}

	@Override
	public List getFaultListForme() {

		return this.sqlSessionTemplate.selectList("inspector.getFaultListForme");
	}

	@Override
	public int deleteFaultList(Integer id) {
		this.sqlSessionTemplate.delete("inspector.deleteFaultList",id);
		int delete=0;
		return delete;
	}

	@Override
	public List getProcessList() {
		return this.sqlSessionTemplate.selectList("inspector.getProcessList");
	}

	@Override
	public int insrtFaultForme(String fault,Integer state) {
		Map map = new HashMap();
		map.put("state", state);
		map.put("fault", fault);
		return this.sqlSessionTemplate.insert("inspector.insrtFaultForme", map);
	}

	@Override
	public List getproductorderListChecked(ProductOrder productOrder) {
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		if(productOrder.getProcessName().equals("前段") || productOrder.getProcessName().equals("中段") || productOrder.getProcessName().equals("后段")){
			
			List<Map<String,Object>> FLst = new ArrayList<Map<String,Object>>();
			
			if (productOrder.getProcessName().equals("前段")) {
				
				productOrder.setFrontType(1);
				
				productOrder.setProcessName(null);
				
				FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckedStageFront",productOrder);
				
			}else if(productOrder.getProcessName().equals("中段")){
				
				productOrder.setMiddleType(1);
				
				productOrder.setProcessName(null);
				
				FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckedStageMiddle",productOrder);
				
			}else if(productOrder.getProcessName().equals("后段")){
				
				productOrder.setAfterType(1);
				
				productOrder.setProcessName(null);
				
				FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckedStageAfter",productOrder);
			}
			
			//FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckedStageAfter",productOrder);
			
			if(FLst != null && FLst.size() > 0){
				
				for(Map<String,Object> FMap : FLst){
					
					Check check = new Check();
					
					check.setProductId(Integer.parseInt(FMap.get("id").toString()));
					
					check.setCheckProcess(productOrder.getCheckProcess());
					check.setCheckPersonId(productOrder.getUserId());
					
					check.setStage(1);
					
					List<Map<String,Object>> checkLst = this.sqlSessionTemplate.selectList("inspector.getCheckIdStage",check);
					
					if(checkLst != null && checkLst.size() > 0){
						
						lst.add(FMap);
					}
				}
			}
		}else if(productOrder.getProcessName().equals("开料") ){
			
			lst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckedKType",productOrder);
			
		}else if(productOrder.getProcessName().equals("制帮")){
			
			lst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckedZType",productOrder);
			
		}else{
			
			lst = this.sqlSessionTemplate.selectList("inspector.getproductorderListChecked",productOrder);
			
		}
		
		return lst;
	}
	
	@Override
	public List getproductorderListCheck(ProductOrder productOrder) {
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		if(productOrder.getStage() > 0){
			
			List<Map<String,Object>> FLst = new ArrayList<Map<String,Object>>();
			
			if (productOrder.getRemarks().equals("前段")) {
				
				productOrder.setFrontType(0);
				
				FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckStageFront",productOrder);
				
			}else if(productOrder.getRemarks().equals("中段")){
				
				productOrder.setMiddleType(0);
				
				FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckStageMiddle",productOrder);
				
			}else if(productOrder.getRemarks().equals("后段")){
				
				productOrder.setAfterType(0);
				
				FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckStageAfter",productOrder);
			}
			
			//FLst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckStage",productOrder);
			
			if(productOrder.getRemarks().equals("中段") || productOrder.getRemarks().equals("后段")){
				
				lst = FLst;
			}else{
				
				if(FLst != null && FLst.size() > 0){
					
					for(Map<String,Object> FMap : FLst){
						
						Check check = new Check();
						
						check.setProductId(Integer.parseInt(FMap.get("id").toString()));
						
						check.setCheckProcess(productOrder.getCheckProcess());
						
						check.setCheckPersonId(productOrder.getUserId());
						
						List<Map<String,Object>> checkLst = this.sqlSessionTemplate.selectList("inspector.getCheckIdStage",check);
						
						if(checkLst == null || checkLst.size() == 0 ){
							
							lst.add(FMap);
						}
					}
				}
				
			}
			
			
		}else{
			
			lst = this.sqlSessionTemplate.selectList("inspector.getproductorderListCheck",productOrder);
			
		}
		return lst;
	}

	@Override
	public List getproductorderListCheckedDetails(int id) {
		
		return this.sqlSessionTemplate.selectList("inspector.getproductorderListCheckedDetails",id);
	}
	
	@Override
	public List<Map<String,Object>> getCheckId(Check check) {
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		if(check.getKzType() > 0){
			//根据生产通知单Id查询帮面信息
			lst = this.sqlSessionTemplate.selectList("inspector.getCheckIdKzType",check);
			
		}else if(check.getStage() > 0){
			//根据生产通知单Id查询成品信息
			lst = this.sqlSessionTemplate.selectList("inspector.getCheckIdStage",check);
			
		}else{
			//根据生产通知单Id查询其它半成品信息
			lst = this.sqlSessionTemplate.selectList("inspector.getCheckId",check);
		}
		
		return lst;
	}
	@Override
	public List<Map<String,Object>> getDefectList(Defect defect) {
		
		return this.sqlSessionTemplate.selectList("inspector.getDefectList",defect);
	}

	@Override
	public void insertDefect(Defect defect) {
		
		this.sqlSessionTemplate.insert("inspector.insertDefect",defect);
		
	}

	@Override
	public void updateCheckTime(Check check) {
		
		this.sqlSessionTemplate.update("inspector.updateCheckTime",check);
		
	}
	@Override
	public Map<String,Object> updateQualifiedCount(ProductOrder productOrder) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		ProductOrder order  = new ProductOrder();
		
		order = this.sqlSessionTemplate.selectOne("productorder.getProductionOrderByRoundAndProductCode", productOrder);
		
		int count = 0;
		
		if (productOrder.getProcessName().equals("前段")) {

			//count = order.getFrontCount() + 1;
			
			productOrder.setFrontCount(productOrder.getNumber());
			
			if(order.getFrontCount()+productOrder.getNumber() > order.getProductCount()){
				
				//map.put("reason", UserConstants.QUALIFIED_GREATER_THAN_PRODUCT);
			}else{
				
				this.sqlSessionTemplate.update("productorder.updateQualifiedCountFront", productOrder);
			}
			
					
		}else if(productOrder.getProcessName().equals("中段")){
			
			//count = order.getMiddleCount() + 1;
			
			productOrder.setMiddleCount(productOrder.getNumber());
			
			if(order.getMiddleCount()+productOrder.getNumber() > order.getProductCount()){
				
				//map.put("reason", UserConstants.QUALIFIED_GREATER_THAN_PRODUCT);
			}else{
				
				this.sqlSessionTemplate.update("productorder.updateQualifiedCountMiddle", productOrder);
			}			
		}else if(productOrder.getProcessName().equals("后段")){
			
			//count = order.getAfterCount() + 1;
			
			productOrder.setAfterCount(productOrder.getNumber());
			
			if(order.getAfterCount()+productOrder.getNumber() > order.getProductCount()){
				
				//map.put("reason", UserConstants.QUALIFIED_GREATER_THAN_PRODUCT);
			}else{
				
				this.sqlSessionTemplate.update("productorder.updateQualifiedCountAlter", productOrder);
			}		
		} 
		
		return map;
	}
//	@Override
//	public Map<String,Object> updateQualifiedCount(ProductOrder productOrder) {
//		
//		Map<String,Object> map = new HashMap<String, Object>();
//		
//		ProductOrder order  = new ProductOrder();
//		
//		order = this.sqlSessionTemplate.selectOne("productorder.getProductionOrderByRoundAndProductCode", productOrder);
//		
//		int count = 0;
//		
//		if (productOrder.getProcessName().equals("前段")) {
//			
//			count = order.getFrontCount() + 1;
//			
//			productOrder.setFrontCount(count);
//			
//			if(order.getFrontCount() >= order.getProductCount()){
//				
//				map.put("reason", UserConstants.QUALIFIED_GREATER_THAN_PRODUCT);
//			}else{
//				
//				this.sqlSessionTemplate.update("productorder.updateQualifiedCountFront", productOrder);
//			}
//			
//			
//		}else if(productOrder.getProcessName().equals("中段")){
//			
//			count = order.getMiddleCount() + 1;
//			
//			productOrder.setMiddleCount(count);
//			
//			if(order.getMiddleCount() >= order.getProductCount()){
//				
//				map.put("reason", UserConstants.QUALIFIED_GREATER_THAN_PRODUCT);
//			}else{
//				
//				this.sqlSessionTemplate.update("productorder.updateQualifiedCountMiddle", productOrder);
//			}			
//		}else if(productOrder.getProcessName().equals("后段")){
//			
//			count = order.getAfterCount() + 1;
//			
//			productOrder.setAfterCount(count);
//			
//			if(order.getAfterCount() >= order.getProductCount()){
//				
//				map.put("reason", UserConstants.QUALIFIED_GREATER_THAN_PRODUCT);
//			}else{
//				
//				this.sqlSessionTemplate.update("productorder.updateQualifiedCountAlter", productOrder);
//			}		
//		} 
//		
//		return map;
//	}
	
	@Override
	public List<Map<String,Object>> getDefectUser(Defect defect) {
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("inspector.getDefectUser", defect);
		
		List<Map<String,Object>> list = this.sqlSessionTemplate.selectList("inspector.getDefect",defect);
		
		for(Map<String, Object> CMap:list){
			
			CMap.put("state", 0);
		}
		
		for(Map<String, Object> CMap:list){
				
			for(Map<String, Object> CUMap:lst){
				
				if(CUMap.get("id").toString().equals(CMap.get("id").toString())){
					
					CMap.put("state", 1);//state为0时则显示选择按钮1则显示取消按钮
				}
			}
		}
		
		return list;
	}
	
	@Override
	public List insertDefectUser(Defect defect) {
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("inspector.getDefectUserById", defect);

		if(lst == null || lst.size() == 0){
			
			this.sqlSessionTemplate.insert("inspector.insertDefectUser",defect);
		}
		
		return this.getDefectUser(defect);
	}
	
	@Override
	public List deleteDefectUser(Defect defect) {

		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("inspector.getDefectUserById", defect);

		if(lst != null && lst.size() > 0){
			
			this.sqlSessionTemplate.delete("inspector.deleteDefectUser",defect);
		}
		
		return this.getDefectUser(defect);
		
	}
	@Override
	public List getDefectAndProcess() {
		
		return this.sqlSessionTemplate.selectList("inspector.getDefectAndProcess");
		
	}
	@Override
	public List getMaxCheckNumber(Check check) {
		
		return this.sqlSessionTemplate.selectList("inspector.getMaxCheckNumber",check);
		
	}

	@Override
	public List<Map<String, Object>> getCallOrDefectAndPictureOrVedio(
			Integer id, Integer state, Integer type) {
		
		Map map = new HashMap();
		map.put("id", id);
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		// 0表示呼叫1表示缺陷
		if(type > 0){
			
			// 0表示图片浏览1表示视频浏览
			if(state > 0){
				
				lst = this.sqlSessionTemplate.selectList("inspector.seeDefectVedio", map);
				
				
			}else{
				
				lst = this.sqlSessionTemplate.selectList("inspector.seeDefectPicture", map);
				
			}
			
		}else{
			// 0表示图片浏览1表示视频浏览
			if(state > 0){
				
				List<Map<String,Object>> callLst = this.sqlSessionTemplate.selectList("inspector.seeCallVedioByCall", map);
				
				if(callLst != null && callLst.size() > 0){
					if(!StringUtil.isEmpty(callLst.get(0).get("viewName").toString())){
						
						map.put("viewName", callLst.get(0).get("viewName").toString());
						
						lst = this.sqlSessionTemplate.selectList("inspector.seeCallVedio", map);
					}
				}
				
			}else{
				
				List<Map<String,Object>> callLst = this.sqlSessionTemplate.selectList("inspector.seeCallPictureByCall", map);
				
				if(callLst != null && callLst.size() > 0){
					
					if(!StringUtil.isEmpty(callLst.get(0).get("pictureName").toString())){
						
						map.put("pictureName", callLst.get(0).get("pictureName").toString());
						
						lst = this.sqlSessionTemplate.selectList("inspector.seeCallPicture", map);
					}
					
				}
			}
		}
		
		return lst;
	}
	
	@Override
	public List<Map<String, Object>> getDefectAndPictureOrVedio(
			String fileName, Integer state, Integer type) {
		
		Map map = new HashMap();
		map.put("fileName", fileName);
		
		List<Map<String,Object>> lst = new ArrayList<Map<String,Object>>();
		
		// 0表示呼叫1表示缺陷
		if(type > 0){
			
			// 0表示图片浏览1表示视频浏览
			if(state > 0){
				
				lst = this.sqlSessionTemplate.selectList("inspector.seeDefectVedio", map);
				
				
			}else{
				
				lst = this.sqlSessionTemplate.selectList("inspector.seeDefectPicture", map);
				
			}
			
		}
		
		return lst;
	}
	
	
	@Override
	public List<Map<String, Object>> getDefectUserLst(Defect defect) {
		
		List<Map<String,Object>> lst = this.sqlSessionTemplate.selectList("inspector.getDefectUser", defect);
		
		return lst;
	}

	@Override
	public void insertSampling(Sampling samplingDto) {
		this.sqlSessionTemplate.insert("inspector.insertSampling", samplingDto);
	}

	@Override
	public List<Map<String, Object>> getCreateSamplingInfo() {
		
		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String, Object>> productTeamLst = this.sqlSessionTemplate.selectList("inspector.getProductTeamForZ");
		
		List<Map<String, Object>> workShopLst = this.sqlSessionTemplate.selectList("planner.getWorkShop");
		
		List<Map<String, Object>> checkPostLst = this.sqlSessionTemplate.selectList("inspector.getCheckPostLst");
		
		map.put("productTeamLst", productTeamLst);
		
		map.put("workShopLst", workShopLst);
		
		map.put("checkPostLst", checkPostLst);
		
		lst.add(map);
		
		return lst;
	}
	
	@Override
	public List<Map<String, Object>> getSamplingLst(Sampling sampling) {
		
		List<Map<String, Object>> productTeamLst = this.sqlSessionTemplate.selectList("inspector.getSamplingLst",sampling);
		
		return productTeamLst;
	}
	
	@Override
	public List<Map<String, Object>> getSamplingDetails(Sampling sampling) {
		
		List<Map<String, Object>> productTeamLst = this.sqlSessionTemplate.selectList("inspector.getSamplingDetails",sampling);
		
		return productTeamLst;
	}
	
	@Override
	public List<Map<String, Object>> getSamplingStatistic(Sampling sampling) {
		
		List<Map<String, Object>> productTeamLst = this.sqlSessionTemplate.selectList("inspector.getSamplingStatistic",sampling);
		
		return productTeamLst;
	}
	@Override
	public void insertCheckPost(Sampling sampling) {
		
		this.sqlSessionTemplate.insert("inspector.insertCheckPost",sampling);
	}
	
	@Override
	public void deleteCheckPost(Sampling sampling) {
		
		this.sqlSessionTemplate.delete("inspector.deleteCheckPost",sampling);
	}
	@Override
	public List<Map<String, Object>> getCheckPostLst() {
		
		return  this.sqlSessionTemplate.selectList("inspector.getCheckPostLst");
	}
}
